home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / appletalk / uab.shar / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-12  |  3.4 KB  |  174 lines

  1. static char rcsid[] = "$Author: cck $ $Date: 88/09/14 10:19:52 $";
  2. static char rcsident[] = "$Header: /src/local/mac/cap/etalk/RCS/log.c,v 1.7 88/09/14 10:19:52 cck Rel $";
  3. static char revision[] = "$Revision: 1.7 $";
  4.  
  5. /*
  6.  * log.c - simple logging facility
  7.  *
  8.  *
  9.  * Copyright (c) 1988 by The Trustees of Columbia University 
  10.  *   in the City of New York.
  11.  *
  12.  * Permission is granted to any individual or institution to use,
  13.  * copy, or redistribute this software so long as it is not sold for
  14.  * profit, provided that this notice and the original copyright
  15.  * notices are retained.  Columbia University nor the author make no
  16.  * representations about the suitability of this software for any
  17.  * purpose.  It is provided "as is" without express or implied
  18.  * warranty.
  19.  *
  20.  * 
  21.  * Edit History:
  22.  *
  23.  *   Aug, 1988 CCKim created
  24.  *
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <varargs.h>
  31. #include "log.h"
  32.  
  33.  
  34. /* current debug level */
  35. static int dlevel;
  36.  
  37. #ifndef TRUE
  38. # define TRUE 1
  39. #endif
  40. #ifndef FALSE
  41. # define FALSE 0
  42. #endif
  43.  
  44. /*
  45.  * set debug level
  46.  *
  47. */
  48. get_debug_level()
  49. {
  50.   return(dlevel);
  51. }
  52. set_debug_level(n)
  53. int n;
  54. {
  55.   dlevel = n;
  56. }
  57.  
  58. /*
  59.  * print message - use vprintf whenever possible (solves the problem
  60.  * of using the varargs macros -- you must interpret the format).
  61.  * This is something all machine should, but don't have :-)
  62.  */
  63.  
  64. static FILE *lfp = stderr;
  65.  
  66.  
  67. #ifdef NOVPRINTF
  68. /* Bletch - gotta do it because pyramids don't work the other way */
  69. /* (using _doprnt and &args) and don't have vprintf */
  70. /* of course, there will be something that is just one arg larger :-) */
  71. /*VARARGS1*/
  72. log(level, fmt, a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,af)
  73. int level;
  74. char *fmt;
  75. #else
  76. /*VARARGS*/
  77. log(va_alist)
  78. va_dcl
  79. #endif
  80. {
  81.   long time();
  82.   char *mytod();
  83. #ifndef NOVPRINTF
  84.   register char *fmt;
  85.   va_list args;
  86.   int level;
  87. #endif
  88.   int saveerr;
  89.   extern int errno;
  90.   extern int sys_nerr;
  91.   extern char *sys_errlist[];
  92.  
  93.   if (lfp == NULL)        /* no logging? */
  94.     return;
  95.  
  96.   saveerr = errno;
  97. #ifndef NOVPRINTF
  98.   va_start(args);
  99.   level = va_arg(args, int);
  100.   fmt = va_arg(args, char *);
  101. #endif
  102.  
  103.   if (dlevel < (level & L_LVL))
  104.     return;
  105.   fprintf(lfp,"uab: %s ",mytod());
  106.  
  107. #ifndef NOVPRINTF
  108.   vfprintf(lfp, fmt, args);
  109.   va_end(args);
  110. #else
  111.   fprintf(lfp, fmt, a1,a2,a3,a4,a5,a6,a7,a8,a9,aa,ab,ac,ad,ae,af);
  112. #endif
  113.   if (level & L_UERR) {
  114.     if (saveerr < sys_nerr)
  115.       fprintf(lfp, ": %s", sys_errlist[saveerr]);
  116.     else
  117.       fprintf(lfp, ": error %d\n", saveerr);
  118.   }
  119.   putc('\n', lfp);
  120.   fflush(lfp);
  121.   if (level & L_EXIT)
  122.     exit(1);
  123. }
  124.  
  125. islogfile()
  126. {
  127.   return(lfp != NULL);
  128. }
  129.  
  130. logfileis(filename, mode)
  131. char *filename;
  132. char *mode;
  133. {
  134.   FILE *fp;
  135.  
  136.   if ((fp = fopen(filename, mode)) != NULL) {
  137.     log(0, "log file name %s", filename);
  138.   } else {
  139.     log(0|L_UERR, "couldn't open logfile %s", filename);
  140.   }
  141.   lfp = fp;            /* reset */
  142. }
  143.  
  144. nologfile()
  145. {
  146.   if (lfp && lfp != stderr)
  147.     fclose(lfp);
  148.   lfp = NULL;
  149. }
  150.  
  151. /*
  152.  * return pointer to formatted tod in static buffer
  153.  *
  154. */
  155. static char *
  156. mytod()
  157. {
  158.   long tloc;
  159.   struct tm *tm, *localtime();
  160.   static char buf[100];        /* should be large enough */
  161.  
  162.   (void)time(&tloc);
  163.   tm = localtime(&tloc);
  164.   if (tm->tm_year > 99)
  165.     sprintf(buf, "%02d:%02d:%02d %02d/%02d/%04d ",
  166.         tm->tm_hour, tm->tm_min, tm->tm_sec,
  167.         tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900);
  168.   else
  169.     sprintf(buf, "%02d:%02d:%02d %02d/%02d/%02d ",
  170.         tm->tm_hour, tm->tm_min, tm->tm_sec,
  171.         tm->tm_mon+1, tm->tm_mday, tm->tm_year);
  172.   return(buf);
  173. }
  174.